package com.example.music_mediaplayer.tool;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by quan on 2018/1/31.
 */

public class SharePreferenceTools {
    private static SharedPreferences sp;
    private static SharedPreferences.Editor editor;
    private static String FILE_NAME = "music_mediaplayer";
    private static SharePreferenceTools sharePreferenceTools;

    /**
     * 单例模式，创建一个SharePreferenceTools实例
     * @param context
     * @return
     */
    public synchronized static SharePreferenceTools getInstance(Context context){
        if (null==sharePreferenceTools){
            sharePreferenceTools = new SharePreferenceTools();
            sp = context.getSharedPreferences(FILE_NAME,context.MODE_PRIVATE);
            editor = sp.edit();
        }
        return sharePreferenceTools;
    }

    /**
     * 构造方法
     */
    public SharePreferenceTools(){}

    public void setString(String key , String value){
        editor.putString(key,value).commit();
    }
    public String getString(String key){
        return sp.getString(key,"");
    }

    public void setBoolean(String key , boolean value){
        editor.putBoolean(key,value).commit();
    }
    public boolean getBoolean(String key){
        return sp.getBoolean(key , false);
    }

    public void setInt(String key , int value){
        editor.putInt(key ,value).commit();
    }
    public int getInt(String key){
        return sp.getInt(key,0);
    }

}
